#!/bin/sh
HOST_NAME=$1
[[ "${HOST_NAME^^}" =~ MASTER.* ]] && IS_MASTER=true  || IS_MASTER=false

echo "#########################################Docker Install Start####################################"
sed -i 's/\r$//' kubeadm_install.sh
chmod u+x docker_install.sh
sh docker_install.sh
echo "#########################################Docker Install END####################################"

echo "#########################################K8s Install Start####################################"
echo "Step 1: init kubernetes yum repo"
sudo bash -c  "cat > /etc/yum.repos.d/kubernetes.repo" <<EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
sudo yum update

echo "Step 2: install kubelet, kubeadm, kubectl"
sudo yum install -y kubelet-1.23.0 kubeadm-1.23.0 kubectl-1.23.0
sudo systemctl enable kubelet

echo "Step 3: set hostname on node"
sudo hostnamectl set-hostname $HOST_NAME
if [ `grep -f /etc/hosts config/kubeadm_host_ip.config | wc -l` == `cat config/kubeadm_host_ip.config | wc -l` ];then
  sudo bash -c "cat config/kubeadm_host_ip.config >> /etc/hosts"
fi

echo "Step 4: config firewall"
if $IS_MASTER;then
  sudo firewall-cmd --permanent --add-port=6443/tcp
  sudo firewall-cmd --permanent --add-port=2379-2380/tcp
  sudo firewall-cmd --permanent --add-port=10250/tcp
  sudo firewall-cmd --permanent --add-port=10251/tcp
  sudo firewall-cmd --permanent --add-port=10252/tcp
  sudo firewall-cmd --permanent --add-port=10255/tcp
  sudo firewall-cmd --reload
else
  sudo firewall-cmd --permanent --add-port=10251/tcp
  sudo firewall-cmd --permanent --add-port=10255/tcp
  sudo firewall-cmd --reload
fi

echo "Step 5: update iptables settings"
sudo bash -c "cat > /etc/sysctl.d/k8s.conf" <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sudo sysctl --system


echo "Step 6: disable SELinux and SWAP"
sudo setenforce 0
sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
sudo sed -i '/swap/d' /etc/fstab
sudo swapoff -a

if $IS_MASTER;then
  echo "Step 7: download kube-system images"
  for r in `sudo kubeadm config images list`
  do
    if [[ "${r}" =~ coredns.* ]];then
      k8s_image=${r//k8s.gcr.io\/coredns/registry.cn-hangzhou.aliyuncs.com/google_containers}
    else
      k8s_image=${r//k8s.gcr.io/registry.cn-hangzhou.aliyuncs.com/google_containers}
    fi
    sudo docker pull $k8s_image
    sudo docker image tag  $k8s_image $r
    sudo docker rmi $k8s_image
  done

  echo "Step 8: kubeadm init"
  sudo kubeadm init --pod-network-cidr=10.244.0.0/16 --image-repository registry.aliyuncs.com/google_containers
  sudo systemctl start kubelet
  echo "Step 9: config kubectl"
  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

  echo "End: install flannel"
  kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
else
  echo "Step 7: Copy  the command like 'sudo kubeadm join --discovery-token cfgrty.1234567890jyrfgd --discovery-token-ca-cert-hash sha256:1234..cdef 1.2.3.4:6443' after kubeadm init from master node and execute it to current worker node"
fi

echo "#########################################K8s Install End####################################"